import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '集团',		name: 'group_id',	type: 'select', loading:true, idStr:'uuid', nameStr:'group_name' },
			{ label: '状态',		name: 'enabled',	type: 'select', loading:true },
			{ label: '公司名称',		name: 'keyword' },
		],
		// 提交表单列表
		// 添加登录名称 20220121 耿琰
		formData : [
			{ label: '公司名称',		name: 'company_name', 	half:true, required:true  },
			{ label: '公司代码',		name: 'comp_num', 		half:true, required:true  },
			{ label: 'Logo',		name: 'logo', 			full:true, type:'upload', code:118 },
			{ label: '公司编号',		name: 'comp_code', 		half:true, required:true },
			{ label: '中文简称',		name: 'short_name', 	half:true },
			{ label: '英文名称',		name: 'en_name', 		half:true },
			{ label: '英文简称',		name: 'short_en_name', 	half:true },
			{ label: '所属集团',		name: 'group_id', 		half:true, required:true, type: 'select', idStr:'uuid', nameStr:'group_name' },
			{ label: '区域',		name: 'areaData', 		half:true, required:true, type: 'address', onRef: ref=> this.address = ref },
			{ label: '地址',		name: 'address', 		half:true, required:true },
			{ label: '英文地址',		name: 'en_address', 	half:true },
			{ label: '法人代表',		name: 'legal_person', 	half:true },
			{ label: '固定电话',		name: 'tel', 			half:true, required:true },
			{ label: '注册日期',		name: 'reg_date', 		half:true, required:true, type:'date-time' },
			{ label: '排序',		name: 'sort', 			half:true, type:'number' },
			{ label: '公司网址',		name: 'website', 		half:true, required:true },
			{ label: '公司简介',		name: 'summary', 		full:true, type:'textarea' },
			{ label: '公司描述',		name: 'description', 		full:true, type:'textarea' },
		]
	}
	
	model = {  }
	
	componentDidMount(){
		const { uuid } = $fn.query()
		if(uuid){
			this.uuid = uuid
			this.query = { group_id:uuid }
		}
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 集团
			$fn.setRows([ forms, formData ], 'group_id', await f.getCache({ api:'corp_group' } ))
			// 设置默认值
			if(this.uuid){ this.searchFormRef.setValue({ group_id: this.uuid }) }
			// 状态
			const enabledData = $fn.setRow(forms, 'enabled', await f.company_state )
			// 区域
			//
			this.setState({ forms, enabledData })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'company/index')
	
	// 表单
	cols = [
		{ title: '公司名称', 	dataIndex: 'company_name', 		width:150 },
		{ title: '公司编码',		dataIndex: 'comp_code', 		width:100 },
		{ title: '公司代码',		dataIndex: 'comp_num', 			width:100 },
		{ title: '简称', 		dataIndex: 'short_name', 		width:150 },
		{ title: '英文名称', 	dataIndex: 'en_name', 			width:150 },
		{ title: '英文简称', 	dataIndex: 'short_en_name', 	width:150, align:'center' },
		{ title: '所属集团', 	dataIndex: 'group_name', 		width:150 },
		{ title: '状态', 		dataIndex: 'enabled', 			...$fn.table.status, render: text => $fn.getState(this.state.enabledData, text) },
		TableControls.controls.call(this,{ width: 200 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled }) => {
		return [
			TableControls.edit.call(this, 'company/info', { uuid }, {
				click: ()=> $fn.disabledRows(this.state.formData, ['comp_code'], true),
				address: value => this.address.load( value )
			}), 																		// 编辑
			(+enabled === -1 || +enabled === 0)  && this.onEnabled(1, [ uuid ]),		// 启用
			(+enabled === 1 || +enabled === 0) && this.onEnabled(0, uuid),				// 禁用
			TableControls.pushTo.call(this, '查看', `department-manage?uuid=${uuid}`),
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: ()=>{
				const { formData } = this.state
				$fn.disabledRows(formData, ['comp_code'], false)
				this.setState({ formData })
			} }),								// 添加
			this.onEnabled(1),					// 启用
			this.uuid && Controls.back.call(this)
		]
	}
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'company/open', off:'company/del', isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='公司管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', scroll:true, width:'60%', height:'80%', api: 'company/add', editApi: 'company/edit', onRefresh:()=>{
					$fn.dataRemove('company/select')
				}}) }
			</>
		)
	}
}

export default Index